<?php
$mysqli = new mysqli('localhost', 'db_user', 'db_password', 'db_name');

if ($mysqli->connect_error) {
    die("数据库连接失败: " . $mysqli->connect_error);
}

$username = $mysqli->real_escape_string($_GET['user']);
$code = $mysqli->real_escape_string($_GET['code']);

$sql = "SELECT * FROM users WHERE username = '$username' AND verification_code = '$code'";
$result = $mysqli->query($sql);

if ($result->num_rows === 0) {
    die("无效的激活链接");
}

$user = $result->fetch_assoc();
$created_time = strtotime($user['created_at']);

if (time() - $created_time > 3600) {
    $mysqli->query("DELETE FROM users WHERE id = " . $user['id']);
    die("激活链接已过期，请重新注册");
}

if ($user['is_activated'] == 0) {
    $update_sql = "UPDATE users SET is_activated = 1 WHERE id = " . $user['id'];
    if ($mysqli->query($update_sql)) {
        echo "账户激活成功！";
    } else {
        echo "激活失败: " . $mysqli->error;
    }
} else {
    echo "账户已激活，无需重复操作";
}

$mysqli->close();
?>